package org.example;
class TreeNode1 {
    int val;

    TreeNode1 left;

    TreeNode1 right;


    TreeNode1(int val) {
        this.val = val;
    }
}
public class Solution1 {
    public int getHeight(TreeNode1 root) {
        //递归出口
        if(root == null) {
            return 0;
        }

        return Math.max(getHeight(root.left),getHeight(root.right)) + 1;
    }

    public boolean isBalanced(TreeNode1 root) {
        //判断一颗二叉树是不是平衡二叉树
        //首先，二叉树左右子树的高度之差的绝对值不超过1，并且二叉树的左子树和右子树都是平衡二叉树
        //如果root为空，则认为其是一颗平衡二叉树
        if(root == null) {
            return true;
        }
        if(Math.abs(getHeight(root.left)-getHeight(root.right)) > 1) {
            return false;
        }

        return isBalanced(root.left) && isBalanced(root.right);
    }

    public static void main(String[] args) {
        TreeNode1 root = null;
        Solution1 solution1 = new Solution1();
        boolean balanced = solution1.isBalanced(root);
        System.out.println(balanced);
    }
}
